/*
 * @Author: heinan
 * @Date: 2023-07-24 00:24:57
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-03 14:57:51
 */
"use strict";
const Service = require("egg").Service;
const { idCreator, passwordCreator } = require("../utils");

class UserService extends Service {
  async find({ username }) {
    return await this.app.mysql.get("login", { username });
  }
  async findEmail({ email }) {
    return await this.app.mysql.get("users", { email });
  }
  async login({ username, password }) {
    const $sql = "select * from login where username=? and password=?";
    const $params = [username, passwordCreator(password)];
    return await this.ctx.app.mysql.query($sql, $params);
  }
  async registry({ username, password }) {
    const $sql = "insert into login (id,username,password) values (?,?,?)";
    const id = idCreator();
    const $params = [id, username, passwordCreator(password)];
    await this.ctx.service.user.add({ id, username });
    return await this.ctx.app.mysql.query($sql, $params);
  }
  // 根据id获得用户身份
  async identity({ id }) {
    const $sql = `select id,login.identity_id,username, identity_text FROM login,identity where login.identity_id=identity.identity_id and login.id=?`;
    return await this.ctx.app.mysql.query($sql, [id]);
  }
  // 根据用户id查询用户信息
  async info({ id }) {
    const $sql = `select nickname,users.username,identity.identity_text,mobile,email,avatar FROM login,users,identity WHERE login.id=users.id AND login.identity_id=identity.identity_id AND login.id=?`;
    const $params = [id];
    return await this.app.mysql.query($sql, $params);
  }
  /**
   * 编辑用户信息
   * @param id String 用户id
   * @param nickname String 昵称
   * @param mobile String 手机号
   * @param email string 邮箱
   * @param avatar string 用户头像
   */
  async update({ id, nickname, mobile, email, avatar }) {
    const $data = { nickname, mobile, email, avatar };
    const $options = { where: { id } };
    return await this.app.mysql.update("users", $data, $options);
  }
  async setuserinfo({ mobile }, uid) {
    const $params = [mobile, uid];
    const $sql = "insert into user (mobile,uid) values (?,?)";
    return await this.ctx.app.mysql.query($sql, $params);
  }

  async addIdentiry({ identity_text, identity_type }) {
    const identity_id = idCreator(identity_text);
    const $params = [identity_id, identity_text, identity_type];
    const $sql =
      "insert into identity (identity_id,identity_text,identity_type) values (?,?,?)";
    return await this.ctx.app.mysql.query($sql, $params);
  }
  // 根据用户的id和api接口判断用户是否具有该权限
  async isUserApiAuthority({ url, method, id }) {
    let sql = ` SELECT * FROM api_authority,identity_api_authority_relation,identity,login 
    where api_authority.api_authority_url='${url}'
    And api_authority.api_authority_method='${method}'
    And api_authority.api_authority_id=identity_api_authority_relation.api_authority_id
    And login.id='${id}'
    And login.identity_id=identity.identity_id 
    And identity.identity_id=identity_api_authority_relation.identity_id`;
    const result = await this.app.mysql.query(sql);
    return result.length > 0;
  }
  /**
   * 获取用户列表
   * @param nickname String 昵称
   * @param username String 用户名
   * @param mobile String 手机号
   * @param currentPage Number 当前页码
   * @param pageSize Number 分页展示个数
   * @returns
   */
  async getUserList({ nickname, username, mobile, currentPage, pageSize }) {
    const where = {};

    if (nickname) {
      where.nickname = nickname;
    }
    if (username) {
      where.username = username;
    }
    if (mobile) {
      where.mobile = mobile;
    }
    const result = await this.app.mysql.select("users", {
      where,
      offset: (currentPage - 1) * pageSize,
      limit: parseInt(pageSize),
    });
    const total = await this.app.mysql.count("users", where);
    return {
      data: result,
      total,
      pageSize: Number(pageSize),
      currentPage: Number(currentPage),
    };
  }
  /**
   * 删除用户信息
   * @param id String 用户id
   */
  async destory({ id }) {
    return await this.app.mysql.delete("users", { id });
  }
  /**
   * 添加用户信息
   * @param id String 用户id
   * @param nickname String 昵称
   * @param username String 用户名
   * @param mobile String 手机号
   * @param email string 邮箱
   * @param avatar string 用户头像
   */
  async add({ id, nickname, username, mobile, email, avatar }) {
    const $data = { id, nickname, username, mobile, email, avatar };
    return await this.app.mysql.insert("users", $data);
  }
  async reset({ id, password }) {
    const $data = { id, password: passwordCreator(password) };
    return await this.app.mysql.update("login", $data);
  }
}

module.exports = UserService;
